package com.rtx.wms.std.handler.cc.down;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.rtx.common.HttpServletRequestUtil;
import com.rtx.model.dto.edi.EdiInnerRequestDTO;
import com.rtx.wms.customer.common.down.DownLoadService;
import com.rtx.wms.std.cc.bean.ApiCcList;
import com.rtx.wms.std.cc.service.ApiCcListService;
import com.rtx.wms.std.common.bean.WmsConstant;
import com.rtx.wms.std.ctclass.ClassHolder;
import com.rtx.wms.std.qm.bean.RtxQMResponse;
import com.rtx.wms.std.so.bean.ApiOrders;
import com.rtx.wms.std.validation.cclist.CclistCheck;

/**
 * 盘点单下发
 */
@SuppressWarnings("all")
public abstract class AbstractCclistDownHandler implements DownLoadService{

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ApiCcListService apiCcListService;
    
	@Autowired
	CclistCheck cclistCheck;
    
    

    public String create(HttpServletRequest request){
        RtxQMResponse res = new RtxQMResponse();
        ApiCcList apiCcList = null;
        try {
        	//处理之前
        	preHandle(request);
        	
        	String requestStr = HttpServletRequestUtil.getHttpBody(request);
    		EdiInnerRequestDTO dto = JSON.parseObject(requestStr, EdiInnerRequestDTO.class);
    		
        	if(StringUtils.hasLength(dto.getConfig().getPreconfig())) {
        		//预处理
            	preHandle(request);
        	}
        	
        	if(apiCcList == null) {
        		String targetJson = dto.getTagetjson();
        		
        		apiCcList = addExternalColumns(targetJson,dto);
        	}
        	
        	cclistCheck.checkEnterpriseConfig(WmsConstant.ENTERPRISE, apiCcList);
        	
        	//处理之后
        	afterHandle(apiCcList,requestStr);
        	
        	//插入之前
        	preinsert(apiCcList,request);
        	
        	//将盘点单下发数据写入wms
        	insertHandle(apiCcList);
        	
            res.setSuccess("成功");
        } catch (Exception e) {
            logger.error("调拨单：{}，创建异常：",e);
            res.setFailure(e.getMessage());
        }
        return JSON.toJSONString(res);
    }
    
    /**
     * 插入表之前处理客制化新增字段
     * @throws InvocationTargetException 
     * @throws Exception 
     * 
     */
    public ApiCcList addExternalColumns(String targetJson,EdiInnerRequestDTO dto){
    	ApiCcList head = null;
    	String apiid =  dto.getConfig().getTargetapiid();
    	Map<String,Class> map = ClassHolder.apiconfigClassMap.get(apiid);
    	if(map != null) {
    		Class headclass =map.get("main");
    		Class subclass =map.get("sub");
    		head = (ApiCcList) JSON.parseObject(targetJson,headclass);
    	}else {
    		head = JSON.parseObject(targetJson,ApiCcList.class);
    	}

    	return head;
    }
    
    protected void insertHandle(ApiCcList apiCcList) {
    	apiCcListService.insert("ris_edi", apiCcList);
    }
    
    //转换字段后处理逻辑
    protected abstract void afterHandle(ApiCcList at,String requestStr);

    /**
     * 进入处理方法之前
     */
    protected abstract ApiOrders preHandle(HttpServletRequest request);
    
    /**
     * 在insert 之后
     * @param request
     * @return
     */
    protected abstract void preinsert(ApiCcList at,HttpServletRequest request);

}
